﻿//--------------------------------------------------------------------------------------------------------------------------------
// <copyright file="RectangularGrid2D.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>Exul</author>
//--------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.FiniteElementMethod
{
    using System;

    /// <summary>Represents rectangular two-dimensional grid.</summary>
    public sealed partial class RectangularGrid2D
    {
        /// <summary>Gets or sets the grid on the first axis.</summary>
        /// <value>The grid on the first axis.</value>
        public Grid1D AxisOne
        {
            get;
            set;
        }

        /// <summary>Gets or sets the grid on the second axis.</summary>
        /// <value>The grid on the second axis.</value>
        public Grid1D AxisTwo
        {
            get;
            set;
        }

        /// <summary>Converts the location in space to location in this grid.</summary>
        /// <param name="location">The location in space.</param>
        /// <param name="allowEmptyLocation">
        ///     If <see langword="true"/>, then left and right borders in the grid can be equal.
        /// </param>
        /// <returns>The location in grid.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="location"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="location"/> is out of this grid.</exception>
        public LocationInGrid GetLocationInGrid(RectangularElementLocation2D location, bool allowEmptyLocation = false)
        {
            var axisOneLocation = this.AxisOne.GetLocationInGrid(location.LocationByFirstAxis, allowEmptyLocation);
            var axisTwoLocation = this.AxisTwo.GetLocationInGrid(location.LocationBySecondAxis, allowEmptyLocation);
            return new LocationInGrid(
                axisOneLocation.LeftBorderPointIndex,
                axisOneLocation.RightBorderPointIndex,
                axisTwoLocation.LeftBorderPointIndex,
                axisTwoLocation.RightBorderPointIndex);
        }
    }
}